package io.leo.leoui.toasts;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

import io.leo.leoui.R;
import io.leo.leoutils.DisplayUtils;

public class ToastTemplate extends Toast {
    TextView view;
    Context context;


    public ToastTemplate(Context context,String msg){
        super(context);
        this.context = context;
        this.view = (TextView) LayoutInflater.from(context).inflate(R.layout.toast_template,null);
        setView(view);
        view.setText(msg);

    }

    protected void setDrawableLeft(int resId){
        Drawable drawable = null;
        drawable=context.getResources().getDrawable(resId);
        drawable.setBounds(0, 0, DisplayUtils.dp2Px(context,20),DisplayUtils.dp2Px(context,20));
        view.setCompoundDrawables(drawable, null, null, null);
    }

    public void setAnimate(int animate){
        try {
            Field mTNField = Toast.class.getDeclaredField("mTN");
            mTNField.setAccessible(true);
            Object mTN = mTNField.get(this);
            Field mParamsFields = mTN.getClass().getDeclaredField("mParams");
            mParamsFields.setAccessible(true);
            WindowManager.LayoutParams mParams = (WindowManager.LayoutParams) mParamsFields.get(mTN);
            mParams.windowAnimations = animate;

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void setText(CharSequence s) {
        view.setText(s);
    }
}
